<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import resize from "@/views/dashboard/mixins/resize";
  import { getStaffPostChartBarTop10 } from "@/api/site/labour/chart";
  import echarts from "echarts";

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      },
      institutionCode: {
        type: String,
        required: true
      }
    },
    data() {
      return {
        chart: null,
        data: [],
        xData: [],
        yData: [],
        visible: false,
      }
    },
    watch: {
      institutionCode: {
        deep: true,
        handler(val) {
          this.institutionCode = val;
          this.setOptions();
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart();
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.setOptions();
      },
      setOptions() {
        this.data = [];
        this.xData = [];
        this.yData = [];
        // 加载数据
        getStaffPostChartBarTop10({institutionCode: this.institutionCode}).then(response => {
          this.visible = true;
          this.data = response.data;
          this.data.forEach(e => {
            this.xData.push(e.name);
            this.yData.push(e.value);
          });
          this.chart = echarts.init(this.$el, 'macarons');
          let option = {
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'shadow'
              }
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            color: ['#00A3E0'],
            xAxis: [
              {
                type: 'category',
                data: this.xData,
                axisTick: {
                  alignWithLabel: true
                }
              }
            ],
            yAxis: [
              {
                type: 'value'
              }
            ],
            series: [
              {
                name: '人数',
                type: 'bar',
                // barWidth: '60%',
                data: this.yData,
                itemStyle: {
                  normal: {
                    //这里是重点
                    color: function(params) {
                      //注意，如果颜色太少的话，后面颜色不会自动循环，最好多定义几个颜色
                      // var colorList = ['#00A3E0','#FFA100', '#ffc0cb', '#CCCCCC', '#BBFFAA','#749f83', '#ca8622', '#ca8622', '#91c7ae','#749f83', '#ca8622'];
                      // return colorList[params.dataIndex]
                    }
                  }
                }
              }
            ]
          };
          this.chart.setOption(option);
          if (response.data.length == 0) {
            this.chart.showLoading({
              text: '暂无数据',
              showSpinner: false,
              textColor: '#808080',
              maskColor: 'rgba(255, 255, 255, 1)',
              fontSize: '26px',
              fontWeight: 'bold'
            })
          } else {
            this.chart.hideLoading();
          }
        });
      }
    }
  }
</script>
